package com.softwaredesign.springboot.service;

import com.softwaredesign.springboot.controller.dto.RoleAuthorityDTO;
import com.softwaredesign.springboot.entity.RoleAuthority;
import com.softwaredesign.springboot.mapper.RoleAuthorityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleAuthorityService {

    @Autowired
    private RoleAuthorityMapper roleAuthorityMapper;

    public List<RoleAuthority> findAll() {
        List<RoleAuthority> roleAuthorities = new ArrayList<>();
        List<RoleAuthorityDTO> roleAuthorityDTOS = roleAuthorityMapper.findAll();
        for(int i = 0; i < roleAuthorityDTOS.size(); i++) {
            Integer roleCode = roleAuthorityMapper.findRoleCodeByID(roleAuthorityDTOS.get(i).getRoleID());
            String roleName = roleAuthorityMapper.findRoleNameByID(roleAuthorityDTOS.get(i).getRoleID());
            Integer authorityCode = roleAuthorityMapper.findAuthorityCodeByID(roleAuthorityDTOS.get(i).getAuthorityID());
            String authorityName = roleAuthorityMapper.findAuthorityNameByID(roleAuthorityDTOS.get(i).getAuthorityID());
            RoleAuthority newRoleAuthority = new RoleAuthority(
                    roleAuthorityDTOS.get(i).getRoleID(),
                    roleCode,
                    roleName,
                    roleAuthorityDTOS.get(i).getAuthorityID(),
                    authorityCode,
                    authorityName);
            roleAuthorities.add(newRoleAuthority);
        }
        return roleAuthorities;
    }

    public int save(RoleAuthorityDTO roleAuthorityDTO) {
        if ((roleAuthorityDTO.getRoleID() < 1 || roleAuthorityDTO.getRoleID() > 32) ||
                (roleAuthorityDTO.getAuthorityID() < 1 || roleAuthorityDTO.getAuthorityID() > 32)) {
            return 0;
        }
        return roleAuthorityMapper.insert(roleAuthorityDTO);
    }

    public List<RoleAuthority> selectPage(Integer roleID) {
        List<RoleAuthority> roleAuthorities = new ArrayList<>();
        List<RoleAuthorityDTO> roleAuthorityDTOS = roleAuthorityMapper.selectPage(roleID);
        for(int i = 0; i < roleAuthorityDTOS.size(); i++) {
            Integer roleCode = roleAuthorityMapper.findRoleCodeByID(roleAuthorityDTOS.get(i).getRoleID());
            String roleName = roleAuthorityMapper.findRoleNameByID(roleAuthorityDTOS.get(i).getRoleID());
            Integer authorityCode = roleAuthorityMapper.findAuthorityCodeByID(roleAuthorityDTOS.get(i).getAuthorityID());
            String authorityName = roleAuthorityMapper.findAuthorityNameByID(roleAuthorityDTOS.get(i).getAuthorityID());
            RoleAuthority newRoleAuthority = new RoleAuthority(
                    roleAuthorityDTOS.get(i).getRoleID(),
                    roleCode,
                    roleName,
                    roleAuthorityDTOS.get(i).getAuthorityID(),
                    authorityCode,
                    authorityName);
            roleAuthorities.add(newRoleAuthority);
        }
        return roleAuthorities;
    }
}
